package jcommon.chap5_collection;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.ChainedTransformer;
import org.apache.commons.lang.ArrayUtils;

public class Transforming {

	public static void main(String[] args) {
		// Create a transformer that multiplies a number by 10
		Transformer multiplyTransform = new Transformer() {
			public Object transform(Object object) {
				Integer integer = (Integer) object;
				return new Integer(integer.intValue() * 10);
			}
		};

		// Create a transformer that subtracts 20 from a number
		Transformer subtractTransform = new Transformer() {
			public Object transform(Object object) {
				Integer integer = (Integer) object;
				return new Integer(integer.intValue() - 20);
			}
		};

		// Chain the transformers together
		Transformer transformChain = new ChainedTransformer(new Transformer[] {
				multiplyTransform, subtractTransform });

		// Create a List of Integer objects
		int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
		List intList = Arrays.asList(ArrayUtils.toObject(array));

		CollectionUtils.transform(intList, transformChain);

		System.out.println("Original: " + ArrayUtils.toString(array));
		System.out.println("Transformed: "
				+ ArrayUtils.toString(intList.toArray()));
	}
}
